package cn.com.winning.ecare.common;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import cn.com.winning.ecare.activity.R;

import android.app.Dialog;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.support.v4.view.ViewPager;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

public class DateAdapter extends BaseAdapter {
	private Dialog mDialog;
	private ListView mListView;
	private List<Date> mList;
	private int itemWidth;
	private int currentMonth;
	private DisplayMetrics display;

	public DateAdapter(Dialog dialog, int month, ListView listview) {
		mDialog = dialog;
		mListView = listview;

		display = dialog.getContext().getResources().getDisplayMetrics();
		itemWidth = Math.round(display.widthPixels / 7);
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.MONTH, month);

		currentMonth = calendar.get(Calendar.MONTH);

		calendar.set(Calendar.DATE, 1);
		int dateOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
		calendar.add(Calendar.DATE, calendar.getFirstDayOfWeek() - dateOfWeek);

		mList = new ArrayList<Date>();
		for (int i = 0; i < 42; i++) {
			mList.add(calendar.getTime());
			calendar.add(Calendar.DATE, 1);
		}

	}

	public int getCount() {

		return 6;
	}

	public Object getItem(int position) {

		return position;
	}

	public long getItemId(int position) {

		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {

		// 选择后日期
		final TextView tvDate = (TextView) mDialog
				.findViewById(R.id.tvSelected);
		// 日历的ViewPager
		final ViewPager mViewPager = (ViewPager) mDialog
				.findViewById(R.id.ViewPager1);
		// 日历上显示的年月
		final TextView tvNY = (TextView) mDialog.findViewById(R.id.top_date);
		convertView = LayoutInflater.from(mDialog.getContext()).inflate(
				R.layout.ssitem, null);

		final int[] itemIds = new int[] { R.id.linearLayout1,
				R.id.linearLayout2, R.id.linearLayout3, R.id.linearLayout4,
				R.id.linearLayout5, R.id.linearLayout6, R.id.linearLayout7 };
		final int[] textIds = new int[] { R.id.textView1, R.id.textView2,
				R.id.textView3, R.id.textView4, R.id.textView5, R.id.textView6,
				R.id.textView7 };
		for (int i = 0; i < textIds.length; i++) {

			TextView textView = (TextView) convertView.findViewById(textIds[i]);
			View itemView = convertView.findViewById(itemIds[i]);
			itemView.setBackgroundColor(mDialog.getContext().getResources().getColor(R.color.white_bg));
			if (i == 3 || i == 4) {
				int addWidth = (display.widthPixels - itemWidth * 7) / 2;
				itemView.setLayoutParams(new LinearLayout.LayoutParams(
						itemWidth + addWidth, itemWidth));
			} else {
				itemView.setLayoutParams(new LinearLayout.LayoutParams(
						itemWidth, itemWidth));
			}
			
			Date date = mList.get(position * 7 + i);
			textView.setText("" + date.getDate());

			// 不是当前月的，设置字体颜色为灰色
			if (currentMonth != date.getMonth()) {
				textView.setTextColor(mDialog.getContext().getResources().getColor(R.color.username_color));
			}else{
				textView.setTextColor(mDialog.getContext().getResources().getColor(R.color.textColor));
			}
			// 设置当日的背景颜色 currentMonth == date.getMonth()
			Date currentDate = new Date();
			if (date.getMonth() == currentDate.getMonth()
					&& date.getDate() == currentDate.getDate()) {
				textView.setBackgroundDrawable(mDialog.getContext().getResources().getDrawable(R.drawable.current_date));
				itemView.setId(0xff);
			}
			// 获取行号
			final int dayPosition = position;

			textView.setOnClickListener(new View.OnClickListener() {

				public void onClick(View v) {
					// TODO Auto-generated method stub
					TextView tv = (TextView) v;
					int index = Arrays.binarySearch(textIds, tv.getId());
					// 获取日期
					Date date = mList.get(dayPosition * 7 + index);
					// 格式化日期
					SimpleDateFormat dateFormate = new SimpleDateFormat(
							"yyyy-MM-dd");
					String dateStr = dateFormate.format(date);
					tvDate.setText("查询日期:" + dateStr);

					// if (tv.getText().toString().equals("")
					// || tv.getText().toString() == null) {
					// } else {
					// tvDate.setText(tvNY.getText().toString()
					// + (tv.getText().length() < 2 ? "0"
					// + tv.getText() : tv.getText()) + "日");
					// }

					// 改变选中项的背景颜色和去掉上一次选中项的颜色
					View itemView = mListView.findViewWithTag(1);
					if (itemView != null) {
						if (itemView.getId() == 0xff) {
						//	itemView.setBackgroundColor(0xff6bd697);
						//	itemView.setBackgroundResource(R.drawable.select_date);
						//	itemView.setBackgroundDrawable(mDialog.getContext().getResources().getDrawable(R.drawable.select_date));
						} else {
							itemView.setBackgroundDrawable(null);
						//	itemView.setBackgroundColor(Color.TRANSPARENT);
						}
						itemView.setTag(0);
					}
					tv.setTag(1);
					tv.setBackgroundDrawable(mDialog.getContext().getResources().getDrawable(R.drawable.select_date));

				}
			});
			
		}

		convertView.setLayoutParams(new ListView.LayoutParams(
				LayoutParams.WRAP_CONTENT, itemWidth));
		return convertView;
	}
}
